import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store"


Vue.use(VueRouter)

//验证某个路由的权限
function checkRoute(path,next){
  if(store.state.info.menus_url.includes(path)){
    next()
  }else{
    next("/")
  }
}
//导出路由
export const IndexRoutes=[
  {
    path: "menu",
    component: () => import("../pages/menu/menu.vue"),
    meta: {
      title: "菜单管理"
    }
  },
  {
    path: "role",
    component: () => import("../pages/role/role.vue"),
    meta: {
      title: "角色管理"
    }
  },
  {
    path: "manage",
    component: () => import("../pages/manage/manage.vue"),
    meta: {
      title: "管理员管理"
    }
  },
  {
    path: "cate",
    component: () => import("../pages/cate/cate.vue"),
    meta: {
      title: "商品分类"
    }
  },
  {
    path: "specs",
    component: () => import("../pages/specs/specs.vue"),
    meta: {
      title: "商品规格"
    }
  },
  {
    path: "goods",
    component: () => import("../pages/goods/goods.vue"),
    meta: {
      title: "商品管理"
    }
  },
  {
    path: "vip",
    component: () => import("../pages/vip/vip.vue"),
    meta: {
      title: "会员管理"
    }
  },
  {
    path: "banner",
    component: () => import("../pages/banner/banner.vue"),
    meta: {
      title: "轮播图管理"
    }
  },
  {
    path: "seckill",
    component: () => import("../pages/seckill/seckill.vue"),
    meta: {
      title: "秒杀活动"
    }
  }
]

const routes = [{
    path: "/login",
    component: () => import("../pages/login/login.vue")
  },
  {
    path: "/",
    component: () => import("../pages/index/index.vue"),
    children: [{
        path: "",
        component: () => import("../pages/home/home.vue"),
      },
      ...IndexRoutes
      

    ]
  }
]

const router = new VueRouter({
  routes
})

//登录拦截
router.beforeEach((to,from,next)=>{
  //如果去登陆，进
  if(to.path=="/login"){
    next()
    return
  }
  console.log(store)
  //去的不是登录，查看是否登录
  if(store.state.info.token){
    next()
    return
  }
  //没有登录
  next("/login")
})

export default router